#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "appinfo.h"
#include "modelserver.h"
#include "wsockclienthelper.h"

#include <QMainWindow>
#include <QSettings>
#include <QDir>
#include <QCoreApplication>
#include <QMenu>
#include <QListWidget>
#include <QMessageBox>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonArray>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void connectToServer(ModelServer info);

private:
    Ui::MainWindow *ui;
    QString m_mypath=QDir::cleanPath(QCoreApplication::applicationDirPath());
    QString m_url;
    WSockClientHelper* m_whelper = nullptr;
    bool isInit = false;
    QMenu* m_rmenu = nullptr;
    int m_index = -1;
    QString m_userName;                       //用户名
    QString m_passWd;                         //密码

private:
    void enableUI(bool isEnable);
    QString genAppDisplayName(AppInfo* info);
    void showAppInfo(AppInfo* info);
    void toggleBtnStatus(AppInfo* info);
    AppInfo* convertJsonToAppInfo(QJsonDocument doc);
    AppInfo* convertJsonToAppInfo(QJsonObject obj);
    QString convertAppInfoToJsonString(AppInfo* info);
    QString convertMapToJsonString(QMap<QString,QString> map);
    void callbackLogin(WSockResponse response);
    void callbackGetAllApps(WSockResponse response);
    void callbackAddApp(WSockResponse response);
    void callbackStartUp(WSockResponse response);
    void callbackStopApp(WSockResponse response);
    void callbackDeleteApp(WSockResponse response);

private slots:
    void slotConnectServer();
    void slotDisconnected();
    void slotError();
    void slotOnReceiveMessage(WSockResponse response);
    void slotLwContextMenu(QPoint);
    void on_actionAddApp_triggered();
    void on_lwApps_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
    void on_txtAppName_textChanged(const QString &arg1);
    void on_chkAutoStart_stateChanged(int arg1);
    void on_txtAppPath_textChanged(const QString &arg1);
    void on_txtArgs_textChanged(const QString &arg1);
    void on_chkAutoRestart_stateChanged(int arg1);
    void on_txtRestartTime_textChanged(const QString &arg1);
    void on_btnBrowse_clicked();
    void on_btnSave_clicked();
    void on_btnStart_clicked();
    void on_btnClose_clicked();
    void on_actionRefresh_triggered();
    void on_actionDeleteApp_triggered();
};
#endif // MAINWINDOW_H
